from django.db import models

from persons.models import Person
from cars.models import Car
from teacher.models import TeacherModel


class VisitorModel(models.Model):
    openid = models.CharField("OPENID", max_length=50, null=True, blank=True)
    dept = models.CharField("单位", max_length=50)
    name = models.CharField("姓名", max_length=50)
    phone = models.CharField("手机", max_length=50, unique=True)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    @property
    def is_authenticated(self):
        return True

    def __str__(self):
        return f"访客<{self.name}>"

    class Meta:
        unique_together = ("openid", "phone")
        verbose_name = verbose_name_plural = "访客"


class VisitorApply(models.Model):
    STATUS_CHOISE = (
        (0, "被访者审核"),
        (1, "上级审核"),
        (2, "保卫处审核"),
        (5, "同意访问"),
        (6, "拒绝访问"),
        (7, "访问中"),
        (8, "完成访问"),
        (9, "访问过期"),
        (10, "访客取消"),
    )
    status = models.IntegerField("状态", choices=STATUS_CHOISE, default=0)
    visitor = models.ForeignKey(
        VisitorModel, on_delete=models.CASCADE, verbose_name="访客用户"
    )
    teacher = models.ForeignKey(
        TeacherModel, on_delete=models.CASCADE, verbose_name="教师"
    )
    what = models.CharField("工作内容", max_length=500)
    when = models.DateTimeField("工作日期")
    isfree = models.IntegerField("是否免停车费", default=0)
    created = models.DateTimeField("创建时间", auto_now_add=True, blank=True, null=True)
    updated = models.DateTimeField("更新时间", auto_now=True, blank=True, null=True)

    def __str__(self):
        return f"{self.visitor.__str__()}在{self.when}申请"

    class Meta:
        verbose_name = verbose_name_plural = "访客访问申请"


class VisitorApplyPerson(models.Model):
    apply = models.ForeignKey(
        VisitorApply, on_delete=models.CASCADE, verbose_name="访客访问申请"
    )
    person = models.ForeignKey(Person, on_delete=models.CASCADE, verbose_name="人员")
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.apply.__str__()}-{self.person.__str__()}"

    class Meta:
        verbose_name = verbose_name_plural = "访客访问申请的人员"


class VisitorApplyCar(models.Model):
    apply = models.ForeignKey(
        VisitorApply, on_delete=models.CASCADE, verbose_name="访客访问申请"
    )
    car = models.ForeignKey(Car, on_delete=models.CASCADE, verbose_name="车辆")
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.apply.__str__()}-{self.car.__str__()}"

    class Meta:
        verbose_name = verbose_name_plural = "访客访问申请的车辆"


class VisitorApplyVerify(models.Model):
    apply = models.ForeignKey(
        VisitorApply, on_delete=models.CASCADE, verbose_name="访客访问申请"
    )
    STATGE_CHOISE = (
        (0, "被访者审核中"),
        (1, "上级审核中"),
        (2, "保卫处审核中"),
        (5, "审核完成"),
        (6, "审核失败"),
        (7, "访问中"),
        (8, "完成访问"),
        (9, "访问过期"),
    )
    stage = models.IntegerField("状态", choices=STATGE_CHOISE, default=0)
    STATUS_CHOISE = (
        (0, "待审核"),
        (1, "审核通过"),
        (2, "审核失败"),
    )
    status = models.IntegerField("结果", choices=STATUS_CHOISE, default=0)
    teacher = models.ForeignKey(
        TeacherModel, on_delete=models.CASCADE, verbose_name="教师"
    )
    describe = models.CharField("描述", max_length=512, blank=True, null=True)
    created = models.DateTimeField("创建时间", auto_now_add=True, blank=True, null=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.apply.__str__()}审核"

    class Meta:
        unique_together = ("apply", "stage")
        verbose_name = verbose_name_plural = "访客访问申请的审核"


# 访客常拜访教师
class VisitorTeacher(models.Model):
    visitor = models.ForeignKey(
        VisitorModel, on_delete=models.CASCADE, verbose_name="访客"
    )
    teacher = models.ForeignKey(
        TeacherModel, on_delete=models.CASCADE, verbose_name="教师"
    )
    order = models.IntegerField("顺序", default=0)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.visitor.__str__()}-{self.teacher.__str__()}"

    class Meta:
        verbose_name = verbose_name_plural = "访客常用教师"


# 访客常用人员
class VisitorPerson(models.Model):
    visitor = models.ForeignKey(
        VisitorModel, on_delete=models.CASCADE, verbose_name="访客"
    )
    person = models.ForeignKey(Person, on_delete=models.CASCADE, verbose_name="人员")
    order = models.IntegerField("顺序", default=0)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.visitor.__str__()}-{self.person.__str__()}"

    class Meta:
        verbose_name = verbose_name_plural = "访客常用人员"


# 访客常用车辆
class VisitorCar(models.Model):
    visitor = models.ForeignKey(
        VisitorModel, on_delete=models.CASCADE, verbose_name="访客"
    )
    car = models.ForeignKey(Car, on_delete=models.CASCADE, verbose_name="车辆")
    order = models.IntegerField("顺序", default=0)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.visitor.__str__()}-{self.car.__str__()}"

    class Meta:
        verbose_name = verbose_name_plural = "访客常用车辆"
